
#include "StdAfx.h"
#include "IniFile.h"

CIniFile::CIniFile()
{
	TCHAR szAppName[MAX_PATH];
	int len;
	::GetModuleFileName(AfxGetInstanceHandle(), szAppName, sizeof(szAppName));
	len = _tcslen(szAppName); 
	for(int i=len; i>0; i--)
	{
		if(szAppName[i] == _T('.'))
		{
			szAppName[i+1]=_T('\0');
			break;
		}
	}
	
	_tcscat(szAppName, _T("ini"));
	
	m_strIniFileName = szAppName;
}

CIniFile::~CIniFile()
{
}

CString CIniFile::GetIniFileName()
{ 
	return m_strIniFileName;
}

void CIniFile::SetIniFileName(CString FileName)
{ 
	m_strIniFileName = FileName;
}

CString CIniFile::GetString(CString AppName,CString KeyName,CString Default)
{
	TCHAR buf[MAX_LENGTH];
	::GetPrivateProfileString(AppName, KeyName, Default, buf, sizeof(buf), m_strIniFileName);
	return buf;
}
double CIniFile::GetDouble(CString AppName,CString KeyName,double Default)
{
	TCHAR buf[MAX_LENGTH];
	CString temp;
	temp.Format(_T("%f"),Default);
	::GetPrivateProfileString(AppName, KeyName, temp, buf, sizeof(buf), m_strIniFileName);
	
	return _tstof(buf);
}

int CIniFile::GetInt(CString AppName,CString KeyName,int Default)
{
	return ::GetPrivateProfileInt(AppName, KeyName, Default, m_strIniFileName);
}

unsigned long CIniFile::GetDWORD(CString AppName,CString KeyName,unsigned long Default)
{
	TCHAR buf[MAX_LENGTH];
	CString temp;
	temp.Format(_T("%u"),Default);
	::GetPrivateProfileString(AppName, KeyName, temp, buf, sizeof(buf), m_strIniFileName);
	return _tstol(buf);
}

BOOL CIniFile::SetString(CString AppName,CString KeyName,CString Data)
{
	return ::WritePrivateProfileString(AppName, KeyName, Data, m_strIniFileName);
}

BOOL CIniFile::SetInt(CString AppName,CString KeyName,int Data)
{
	CString temp;
	temp.Format(_T("%d"),Data);
	return ::WritePrivateProfileString(AppName, KeyName, temp, m_strIniFileName);
}

BOOL CIniFile::SetDouble(CString AppName,CString KeyName,double Data)
{
	CString temp;
	temp.Format(_T("%f"),Data);
	return ::WritePrivateProfileString(AppName, KeyName, temp, m_strIniFileName);
}

BOOL CIniFile::SetDWORD(CString AppName,CString KeyName,unsigned long Data)
{
	CString temp;
	temp.Format(_T("%u"),Data);
	return ::WritePrivateProfileString(AppName, KeyName, temp, m_strIniFileName);
}